package com.zds.scm.common.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.apache.log4j.Logger;

/**
 * 
 * 2008-12-1
 * 
 * @author <a href="mailto:yhuaming@hotmail.com">yhm</a>
 * 
 */
public final class JdbcUtils {
	/**
	 * Logger for this class
	 */
	private static final Logger	logger			= Logger.getLogger(JdbcUtils.class);
	
	private static String		driverClassName	= "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static String		connURL			= "jdbc:sqlserver://127.0.0.1:1433;databaseName=shoppingdb";
	private static String		userName		= "sa";
	private static String		passWord		= "";
	private static String		jndiName	= "java:comp/env/jdbc/shoppingdb";// 容器中数据源Jndi名称
	private static Properties prop=null;
	/**
	 * 静态初始化块，整个类只执行一次。装入配置参数文件
	 */
	static {
		try {
			prop = new Properties();
			InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbcconfig.properties");
			prop.load(is);
			driverClassName = prop.getProperty("driverClassName", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
			connURL = prop.getProperty("url", "jdbc:sqlserver://127.0.0.1:1433;databaseName=shoppingdb");
			userName = prop.getProperty("username", "sa");
			passWord = prop.getProperty("password", "select*");
			jndiName = prop.getProperty("jndiName", "");
			is.close();
			Class.forName(driverClassName);
			
			logger.debug("读取配置文件初始化数据库驱动成功");
		} catch (Exception ex) {
			logger.error("读取配置文件初始化数据库驱动错误" + ex.getMessage());
			ex.printStackTrace();
		}
	}
	
	/**
	 * 获取一个数据库连接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			logger.debug("创建数据库连接");
			return DriverManager.getConnection(connURL, userName, passWord);
		} catch (Exception ex) {
			logger.error("获取数据库连接异常" + ex.getMessage());
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 从容器获取数据源
	 * 
	 * @return
	 */
	public static DataSource getDataSource() {
		Context ctx = null;
		DataSource ds = null;
		try {
			ctx = new InitialContext();
			ds = (DataSource) ctx.lookup(jndiName);
		} catch (NamingException e) {
			logger.error("获取数据源失败");
			e.printStackTrace();
		}
		return ds;
	}
	/**
	 * 获取数据源
	 * 
	 * @return
	 */
	public static DataSource getDataSourceA() {
		DataSource ds = null;
		try {			
			ds = BasicDataSourceFactory.createDataSource(prop);
		} catch (Exception e) {
			logger.error("获取数据源失败");
			e.printStackTrace();
		}
		return ds;
	}
	/**
	 * 从数据库连接池获取连接
	 * 
	 * @return
	 */
	public static Connection getConnectionfromDataSource() {
		try {
			return getDataSource().getConnection();
		} catch (SQLException e) {
			logger.error("从数据源获取连接失败");
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 释放数据库资源
	 * 
	 * @param rs
	 * @param st
	 * @param conn
	 */
	public static void free(ResultSet rs, Statement st, Connection conn) {
		try {
			if (rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (st != null)
					st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (conn != null)
					try {
						conn.close();
						logger.debug("释放数据库连接");
					} catch (SQLException e) {
						e.printStackTrace();
					}
			}
		}
	}
	public static String getDriverClassName() {
		return driverClassName;
	}
	private JdbcUtils() {
	}
}
